
package definePresent.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


import model.ConnectionPool;
import model.DatabaseException;


public class PresentBean {
	private String description;
	private String figurePath;
	private int count;
	public PresentBean(String description, int count){
		this.description = description;
		this.count = count;
	}
	public void setFigure(String figurePath){
		this.figurePath = figurePath;
	}
	public boolean isFigureUploaded(){
		boolean isFigureUploaded = false;
		if (figurePath!=null){
			isFigureUploaded = true;
		}
		return isFigureUploaded;
	}
	
	private  void insertIntoPresents(Connection con, long l) throws SQLException{
		String query = "insert into Presents(description,figure_path, count, supplier_id) values(?,?,?,?)";
		try (PreparedStatement preparedStatement = con.prepareStatement(query);) {
			preparedStatement.setString(1, description);
			preparedStatement.setString(2, figurePath);
			preparedStatement.setLong(3, count);
			preparedStatement.setLong(4, l);
			preparedStatement.executeUpdate();
		}

	}
	public void addToDB(long l) throws DatabaseException{
		Connection con=null;
		try {
			con = ConnectionPool.getConnection();
			con.setAutoCommit(false);
			insertIntoPresents(con, l);
			con.commit();
		} catch (SQLException e) {
			try {
				if(con!=null){
					con.rollback();
				}
			} catch (SQLException e1) {

				throw new DatabaseException(e1);
			}
			throw new DatabaseException(e);
		}finally{
			try {
				if(con!=null){
					con.close();
				}
			} catch (SQLException e) {
				throw new DatabaseException(e);
			}
		}
	}
}
